package com.itheima;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.function.Consumer;

public class HashSetDemo {
    public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<>();
        list.add("Zhangsan");
        list.add("Zhangsan");
        list.add("Zhangsan");
        list.add("Zhangsan");
        System.out.println(list);

        ArrayList<String> newlist = new ArrayList<>((new HashSet<String>(list)));//去重
        System.out.println(newlist);

        System.out.println(new HashSet<String>(list));//去重

        // 1.创建一个 HashSet 对象
        HashSet<String> hashSet = new HashSet<>();

        // 2.添加元素
        hashSet.add("Zhangsan");
        hashSet.add("Lisi");
        hashSet.add("Lisi");//重复元素
        hashSet.add("Wangwu");
        hashSet.add("Laoliu");

        System.out.println(hashSet);

        //3.取数据(迭代器)
        Iterator<String> iterator = hashSet.iterator();
        while(iterator.hasNext()){
            String next = iterator.next();
            System.out.println(next);
        }

        //4.取数据(增强for)for(声明变量:集合对象)
        for (String name : hashSet) {
            System.out.println("name="+name);
        }

        int[] arr={1,2,3,4,5};
        for (int i : arr) {
            System.out.println("i="+i);
        }

        //5.取数据(lambda表达式)
        hashSet.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });

        hashSet.forEach(s->System.out.println(s));

        hashSet.forEach(System.out::println);



        Student student1 = new Student("Zhangsan", 20);
        //toString
        System.out.println(student1);
    }
}